package com.tanhua.manager.controller;

import com.tanhua.manager.service.UsersFreezeService;
import com.tanhua.manager.vo.PageResult;
import com.tanhua.manager.vo.UserInformation;
import com.tanhua.manager.vo.UsersFreezeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("manage/users")
public class UsersFreezeController {

    @Autowired
    private UsersFreezeService usersFreezeService;

    /**
     * 查询用户信息
     *
     * @param
     * @return
     */
    @GetMapping("{userID}")
    public ResponseEntity<UserInformation> profile( @PathVariable("userID") String userID) {
        //调用业务层
        UserInformation userInformation = usersFreezeService.queryUserInformation(userID);
        if (null != userInformation) {
            return ResponseEntity.ok(userInformation);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 用户数据翻页
     * @param page
     * @param pagesize
     * @param id
     * @param nickname
     * @param city
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> UserPagesList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pagesize", defaultValue = "20") Integer pagesize,
                                                    @RequestParam("id") String id,
                                                    @RequestParam("nickname") String nickname,
                                                    @RequestParam("city") String city){
        try {
            PageResult pageResult = this.usersFreezeService.UserPagesList(page, pagesize, id, nickname, city);
            return ResponseEntity.ok(pageResult);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 冻结用户
     *
     * @return
     */
    @PostMapping("freeze")
    public ResponseEntity<String> saveFreeze(@RequestBody Map<String,String> param) {
        Integer userId = Integer.valueOf(param.get("userId"));//获取冻结用户id
        Integer freezingTime = Integer.valueOf(param.get("freezingTime"));//获取冻结时间
        Integer freezingRange = Integer.valueOf(param.get("freezingRange"));//获取冻结范围
        String reasonsForFreezing = param.get("reasonsForFreezing");//获取冻结原因
        String frozenRemarks = param.get("frozenRemarks");//获取冻结备注

        Boolean bool = this.usersFreezeService.saveFreeze(userId,freezingTime,freezingRange,reasonsForFreezing,frozenRemarks);
        if (bool) {
            return ResponseEntity.ok("冻结用户操作成功");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 解冻用户
     *
     * @return
     */
    @PostMapping("unfreeze")
    public ResponseEntity<String> unfreeze(@RequestBody Map<String, String> params) {
        Boolean bool = this.usersFreezeService.unfreeze(params);
        if (bool) {
            return ResponseEntity.ok("解冻用户操作成功");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
